# ------------------------------------- #
# Replication code for:
#
# Pomeroy, Caleb "The Damocles Delusion: The Sense of Power Inflates Threat  
# Perception in World Politics," International Organization.
#
# This script reproduces the embedding analysis of US Cold War foreign policy elites.
# ------------------------------------- #

# --- set your working directory --- #
setwd("~/Downloads/damocles_delusion_replication/")

# --- libraries --- #
library(ggplot2)
library(texreg)
library(lsa)
library(dplyr)
set.seed(1912)

# --- file directory
# the data consist of twenty embedding models fitted to resamples of the underlying FRUS corpus
# each .rds file contains a unique term x 300 coordinate matrix
# here, simply specify the file path to later loop through the estimates
file_direct <- "data/word_embedding_resamples/"
files_list <- list.files(file_direct) 

# --- define dictionary terms --- #
# define terms to measure the various constructs of interest
soviet_threat_words <- c("soviet", "russian", "aggress", "threat", "hostil")
china_threat_words <- c("china", "chines", "aggress", "threat", "hostil")
vietcong_threat_words <- c("vietcong", "north", "vietnam", "aggress", "threat", "hostil")
intuition_words <- c("sens", "feel", "impress", "instinct")
power_words <- c("abl", "capac", "capabl", "strong", "stronger", "power")
weak_words <- c("unabl", "incapacit", "incap", "weak", "weaker", "powerless") 
our_words <- c("us", "we", "our") # to make clear talking about US, and not Soviet, power

# --- additional dictionaries for the supplementary analyses
deliberation_words <- c("calcul", "study", "diagnos", "examin", "investig") 
power_words_dynamic <- c("power", "rise") 
weak_words_dynamic <- c("powerless", "declin") 

# --- main cosine estimates --- #
# the basic procedure is as follows: (1) calculate the cosine sim between the "our power" terms and various threat perception terms
# then (2) calculate the cosine sim between the "our powerless" terms and various threat perception terms
# then (3) take the difference between these two quantities; akin to a difference in difference estimate, higher values indicate power terms 
# associate more strongly with threat perception terms than weakness terms associate with threat perception terms
results_frus <- data.frame()
for(i in 1:length(files_list)){

  word_vectors_i <- readRDS(paste(file_direct,files_list[i], sep = "")) # load in each embedding resample

  # --- power and soviet threat perception
  soviet_df_power <- data.frame()
  for(k in 1:length(power_words)){
    for(j in 1:length(soviet_threat_words)){
      soviet_df_power <- rbind(soviet_df_power,
                               data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                      word_vectors_i[power_words[k],])),
                                                       word_vectors_i[soviet_threat_words[j],]),
                                          term = power_words[k],
                                          term_threat = soviet_threat_words[j]))
    }
  }
  
  soviet_df_weak <- data.frame()
  for(k in 1:length(weak_words)){
    for(j in 1:length(soviet_threat_words)){
      soviet_df_weak <- rbind(soviet_df_weak,
                              data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                     word_vectors_i[weak_words[k],])),
                                                      word_vectors_i[soviet_threat_words[j],]),
                                         term = weak_words[k],
                                         term_threat = soviet_threat_words[j]))
    }
  }
  
  # --- power and china threat perception
  china_df_power <- data.frame()
  for(k in 1:length(power_words)){
    for(j in 1:length(china_threat_words)){
      china_df_power <- rbind(china_df_power,
                              data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                     word_vectors_i[power_words[k],])),
                                                      word_vectors_i[china_threat_words[j],]),
                                         term = power_words[k],
                                         term_threat = china_threat_words[j]))
    }
  }
  
  china_df_weak <- data.frame()
  for(k in 1:length(weak_words)){
    for(j in 1:length(china_threat_words)){
      china_df_weak <- rbind(china_df_weak,
                             data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                    word_vectors_i[weak_words[k],])),
                                                     word_vectors_i[china_threat_words[j],]),
                                        term = weak_words[k],
                                        term_threat = china_threat_words[j]))
    }
  }
  
  # --- power and vietcong threat perception
  vietcong_df_power <- data.frame()
  for(k in 1:length(power_words)){
    for(j in 1:length(vietcong_threat_words)){
      vietcong_df_power <- rbind(vietcong_df_power,
                                 data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                        word_vectors_i[power_words[k],])),
                                                         word_vectors_i[vietcong_threat_words[j],]),
                                            term = power_words[k],
                                            term_threat = vietcong_threat_words[j]))
    }
  }
  
  vietcong_df_weak <- data.frame()
  for(k in 1:length(weak_words)){
    for(j in 1:length(vietcong_threat_words)){
      vietcong_df_weak <- rbind(vietcong_df_weak,
                                data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                       word_vectors_i[weak_words[k],])),
                                                        word_vectors_i[vietcong_threat_words[j],]),
                                           term = weak_words[k],
                                           term_threat = vietcong_threat_words[j]))
    }
  }
  
  # --- power and intuition
  df_power_intuition <- data.frame()
  for(k in 1:length(power_words)){
    for(j in 1:length(intuition_words)){
      df_power_intuition <- rbind(df_power_intuition,
                                          data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                 word_vectors_i[power_words[k],])),
                                                                  word_vectors_i[intuition_words[j],]),
                                                     term = power_words[k],
                                                     term_threat = intuition_words[j]))
    }
  }
  
  df_weak_intuition <- data.frame()
  for(k in 1:length(weak_words)){
    for(j in 1:length(intuition_words)){
      df_weak_intuition <- rbind(df_weak_intuition,
                                         data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                word_vectors_i[weak_words[k],])),
                                                                 word_vectors_i[intuition_words[j],]),
                                                    term = weak_words[k],
                                                    term_threat = intuition_words[j]))
    }
  }
  
  results_frus <- rbind(results_frus,
                        data.frame(cos_est = c(mean(soviet_df_power$est - soviet_df_weak$est), 
                                               mean(china_df_power$est - china_df_weak$est),
                                               mean(vietcong_df_power$est - vietcong_df_weak$est),
                                               mean(df_power_intuition$est - df_weak_intuition$est)), 
                                   term = c("Soviet\nThreat", "China\nThreat", "Vietcong\nThreat", "Intuitive\nThinking"),
                                   resample_i = i))
  
}

# summarize results, taking second smallest and 19th largest estimates as CI endpoints
plot_df <- 
  results_frus %>% group_by(term) %>% 
  dplyr::summarise(mean_cos=mean(cos_est),
            ci_high = sort(cos_est)[19],
            ci_low = sort(cos_est)[2])
plot_df$model <- "main"
plot_df$term <- factor(plot_df$term, levels = c("Intuitive\nThinking",  "Vietcong\nThreat", "China\nThreat", "Soviet\nThreat")) 

# --- mechanistic estimates --- #
# below, I operationalize the intuition of Acharya, Blackwell, and Sen's (2018) average controlled direct effect in the context of embeddings
# the basic idea is to average the power and weakness terms with intuition terms; this holds intuition constant across power levels
# and therefore "blocks" the role of the intuition words; cosine estimates between power and threat perception should shrink if intuition helps to explain the associations between power and threat
threat_list <- list("Soviet\nThreat" = soviet_threat_words, 
                    "China\nThreat" = china_threat_words, 
                    "Vietcong\nThreat" = vietcong_threat_words)
results_frus <- data.frame()
for(p in 1:length(threat_list)){
  for(i in 1:length(files_list)){
    
    word_vectors_i <- readRDS(paste(file_direct,files_list[i], sep = ""))
    threat_words <- threat_list[[p]]

    # --- power and threat perception
    df_power <- data.frame()
    for(k in 1:length(power_words)){
      for(j in 1:length(threat_words)){
        df_power <- rbind(df_power,
                                  data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                         word_vectors_i[power_words[k],])),
                                                          word_vectors_i[threat_words[j],]),
                                             term = power_words[k],
                                             term_threat = threat_words[j]))
      }
    }
    
    df_weak <- data.frame()
    for(k in 1:length(weak_words)){
      for(j in 1:length(threat_words)){
        df_weak <- rbind(df_weak,
                                 data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                        word_vectors_i[weak_words[k],])),
                                                         word_vectors_i[threat_words[j],]),
                                            term = weak_words[k],
                                            term_threat = threat_words[j]))
      }
    }
    
    # --- power and threat perception, fixing intuition
    df_power_intuition <- data.frame()
    for(k in 1:length(power_words)){
      for(j in 1:length(threat_words)){
        df_power_intuition <- rbind(df_power_intuition,
                                            data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                   colMeans(word_vectors_i[intuition_words,]),
                                                                                   word_vectors_i[power_words[k],])),
                                                                    word_vectors_i[threat_words[j],]),
                                                       term = power_words[k],
                                                       term_threat = threat_words[j]))
      }
    }
    
    df_weak_intuition <- data.frame()
    for(k in 1:length(weak_words)){
      for(j in 1:length(threat_words)){
        df_weak_intuition <- rbind(df_weak_intuition,
                                           data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                  colMeans(word_vectors_i[intuition_words,]),
                                                                                  word_vectors_i[weak_words[k],])),
                                                                   word_vectors_i[threat_words[j],]),
                                                      term = weak_words[k],
                                                      term_threat = threat_words[j]))
      }
    }
    
    results_frus <- rbind(results_frus,
                          data.frame(cos_est = c(mean(df_power$est - df_weak$est), 
                                                 mean(df_power_intuition$est - df_weak_intuition$est)), 
                                     term = c("Threat Perception",  "Threat Perception +\nIntuitive Thinking"), 
                                     threat_type = c(paste(names(threat_list)[p]), paste(names(threat_list)[p])), 
                                     resample_i = i))
    
  }}

# summarize results, taking second smallest and 19th largest estimates as CI endpoints
plot_df2 <- 
  results_frus %>% group_by(term, threat_type) %>% 
  dplyr::summarise(mean_cos=mean(cos_est),
            ci_high = sort(cos_est)[19],
            ci_low = sort(cos_est)[2])
plot_df2$model <- "acde"
plot_df2$term <- factor(plot_df2$term, levels = c("Threat Perception +\nIntuitive Thinking", "Threat Perception")) 

# --- format for plotting (figure 4A of main text)
plot_df$type <- c("Threat\nPerception", "Cognitive\nStyle", "Threat\nPerception", "Threat\nPerception")
plot_df$type <- factor(plot_df$type, levels = c("Threat\nPerception", "Cognitive\nStyle"))
plot_df$major <- "Main Cosine\nEstimates"
plot_col <- ifelse(plot_df$type == "Threat\nPerception", "#54545d", "#99998e")
(p1 <- 
    ggplot(plot_df, aes(x = mean_cos, y = term, color = plot_col)) +
    geom_vline(xintercept = 0, linetype = "solid", color = "black", size = 1) +
    geom_linerange(aes(xmin = ci_low, xmax = ci_high), size = 3.2, color = plot_col) +
    geom_point(size = 7.5, color = "black") +
    geom_point(size = 6, color = plot_col) +
    facet_grid(type ~ major,scales = "free", space = "free_y") +
    scale_x_continuous(limits = c(-.01, .092)) +
    theme_bw() +
    labs(x = "Sense of US Power", y=NULL) +
    theme(axis.title.x = element_text(size = 16, margin = margin(t=15)),
          axis.text.y = element_text(size = 16, color="black"),
          axis.text.x = element_text(size = 13),
          strip.text.x = element_text(size = 16, margin = margin(r=8,l=8,t=8,b=8)),
          strip.text.y = element_text(size = 15),
          legend.position = "none",
          panel.spacing = unit(1.2, "lines"),
          panel.background = element_rect(fill = "#f6f6f5"),
          strip.background = element_rect(fill = "gray90", color = "black", size = 1.5),
          plot.margin = margin(r = 8, l = 5, t = 5)))

# --- format for plotting (figure 4B of main text)
plot_df2$term <- factor(plot_df2$term, levels = rev(c("Threat Perception", "Threat Perception +\nIntuitive Thinking")))
plot_df2$threat_type <- factor(plot_df2$threat_type, levels = c("Vietcong\nThreat", "China\nThreat", "Soviet\nThreat"))
plot_df2$major <- "Estimates Fixing\nIntuitive Thinking"
(p2 <- 
    ggplot(plot_df2, aes(x = mean_cos, y = threat_type, colour = term)) +
    geom_vline(xintercept = 0, linetype = "solid", color = "black", size = 1) +
    geom_linerange(aes(xmin = ci_low, xmax = ci_high), size = 3.2, position = position_dodge(width = .5)) +
    geom_point(size = 7.5, colour = "black", aes(group = term),  position = position_dodge(width = .5)) +
    geom_point(size = 6, position = position_dodge(width = .5)) +
    theme_bw() +
    scale_color_manual(values = c( "gray78", "#99998e", "#54545d")) +
    facet_wrap(major ~.) +
    scale_x_continuous(limits = c(-.01, .092)) +
    labs(x = "Sense of US Power", y=NULL) +
    theme(axis.title.x = element_text(size = 16, margin = margin(t=15)),
          axis.text.y = element_text(size = 16, color="black"),
          axis.text.x = element_text(size = 13),
          strip.text.x = element_text(size = 16, margin = margin(r=8,l=8,t=8,b=8)),
          panel.spacing = unit(1.2, "lines"),
          legend.title = element_blank(),
          legend.text = element_text(size = 14),
          legend.spacing.y = unit(.5, "cm"),
          legend.justification = "top",
          panel.background = element_rect(fill = "#f6f6f5"),
          strip.background = element_rect(fill = "gray90", color = "black", size = 1.5),
          plot.margin = margin(l = 8, t = 5)) +
    guides(color = guide_legend(reverse = TRUE, byrow = TRUE)))

# --- figure 4 from the main text
(final_p <- 
    ggpubr::ggarrange(p1, p2,
                      labels = c("A", "B"),
                      widths = c(.7,1),
                      font.label = list(face = "bold", size = 22),
                      hjust = -1,
                      ncol = 2, nrow = 1))

# --- appendix table A12
embedding_table_main_static <- createTexreg(coef.names = c("Soviet Threat", "China Threat", "Vietcong Threat", "Intuitive Thinking"),
                            coef = c(subset(plot_df, term == "Soviet\nThreat")$mean_cos, subset(plot_df, term == "China\nThreat")$mean_cos, 
                                     subset(plot_df, term == "Vietcong\nThreat")$mean_cos, subset(plot_df, term == "Intuitive\nThinking")$mean_cos),
                            ci.low = c(subset(plot_df, term == "Soviet\nThreat")$ci_low, subset(plot_df, term == "China\nThreat")$ci_low, 
                                       subset(plot_df, term == "Vietcong\nThreat")$ci_low, subset(plot_df, term == "Intuitive\nThinking")$ci_low),
                            ci.up = c(subset(plot_df, term == "Soviet\nThreat")$ci_high, subset(plot_df, term == "China\nThreat")$ci_high, 
                                      subset(plot_df, term == "Vietcong\nThreat")$ci_high, subset(plot_df, term == "Intuitive\nThinking")$ci_high),
                            model.name = "Main Cosine Estimates")
screenreg(embedding_table_main_static,
          digits = 3)
# texreg(embedding_table_main_static,
#        booktabs = T,
#        caption.above = T,
#        digits = 3,
#        caption = "Cosine Estimates: Relationship Between Power Terms, Threat Perception, and Intuitive Thinking Terms")

# --- appendix table A13
embedding_table_acde_static <- createTexreg(coef.names = c("Soviet Threat + Intuition", "China Threat + Intuition", "Vietcong Threat + Intuition"),
                                            coef = c(subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Soviet\nThreat")$mean_cos, 
                                                     subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "China\nThreat")$mean_cos, 
                                                     subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Vietcong\nThreat")$mean_cos),
                                            ci.low = c(subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Soviet\nThreat")$ci_low, 
                                                       subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "China\nThreat")$ci_low, 
                                                       subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Vietcong\nThreat")$ci_low),
                                            ci.up = c(subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Soviet\nThreat")$ci_high, 
                                                      subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "China\nThreat")$ci_high, 
                                                      subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Vietcong\nThreat")$ci_high),
                                            model.name = "Cosine Estimates Fixing Intuitive Thinking")
screenreg(embedding_table_acde_static,
          digits = 3)
# texreg(embedding_table_acde_static,
#        booktabs = T,
#        caption.above = T,
#        digits = 3,
#        caption = "Cosine Estimates: Relationship Between Power Terms and Threat Perception, Fixing Intuitive Thinking")


# --- main cosine estimates (supplementary rise/decline analysis) --- #
# here, we do the same analyses as above except (1) focus on explicitly rising/declining power and (2) analyze deliberation terms in addition to intuition terms
results_frus <- data.frame()
for(i in 1:length(files_list)){
  
  word_vectors_i <- readRDS(paste(file_direct,files_list[i], sep = ""))

  # --- power and soviet threat perception
  soviet_df_power <- data.frame()
  for(k in 1:length(power_words_dynamic)){
    for(j in 1:length(soviet_threat_words)){
      soviet_df_power <- rbind(soviet_df_power,
                               data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                      word_vectors_i[power_words_dynamic[k],])),
                                                       word_vectors_i[soviet_threat_words[j],]),
                                          term = power_words_dynamic[k],
                                          term_threat = soviet_threat_words[j]))
    }
  }
  
  soviet_df_weak <- data.frame()
  for(k in 1:length(weak_words_dynamic)){
    for(j in 1:length(soviet_threat_words)){
      soviet_df_weak <- rbind(soviet_df_weak,
                              data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                     word_vectors_i[weak_words_dynamic[k],])),
                                                      word_vectors_i[soviet_threat_words[j],]),
                                         term = weak_words_dynamic[k],
                                         term_threat = soviet_threat_words[j]))
    }
  }
  
  # --- power and china threat perception
  china_df_power <- data.frame()
  for(k in 1:length(power_words_dynamic)){
    for(j in 1:length(china_threat_words)){
      china_df_power <- rbind(china_df_power,
                              data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                     word_vectors_i[power_words_dynamic[k],])),
                                                      word_vectors_i[china_threat_words[j],]),
                                         term = power_words_dynamic[k],
                                         term_threat = china_threat_words[j]))
    }
  }
  
  china_df_weak <- data.frame()
  for(k in 1:length(weak_words_dynamic)){
    for(j in 1:length(china_threat_words)){
      china_df_weak <- rbind(china_df_weak,
                             data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                    word_vectors_i[weak_words_dynamic[k],])),
                                                     word_vectors_i[china_threat_words[j],]),
                                        term = weak_words_dynamic[k],
                                        term_threat = china_threat_words[j]))
    }
  }
  
  # --- power and vietcong threat perception
  vietcong_df_power <- data.frame()
  for(k in 1:length(power_words_dynamic)){
    for(j in 1:length(vietcong_threat_words)){
      vietcong_df_power <- rbind(vietcong_df_power,
                                 data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                        word_vectors_i[power_words_dynamic[k],])),
                                                         word_vectors_i[vietcong_threat_words[j],]),
                                            term = power_words_dynamic[k],
                                            term_threat = vietcong_threat_words[j]))
    }
  }
  
  vietcong_df_weak <- data.frame()
  for(k in 1:length(weak_words_dynamic)){
    for(j in 1:length(vietcong_threat_words)){
      vietcong_df_weak <- rbind(vietcong_df_weak,
                                data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                       word_vectors_i[weak_words_dynamic[k],])),
                                                        word_vectors_i[vietcong_threat_words[j],]),
                                           term = weak_words_dynamic[k],
                                           term_threat = vietcong_threat_words[j]))
    }
  }
  
  # --- power and intuition
  df_power_intuition <- data.frame()
  for(k in 1:length(power_words_dynamic)){
    for(j in 1:length(intuition_words)){
      df_power_intuition <- rbind(df_power_intuition,
                                          data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                 word_vectors_i[power_words_dynamic[k],])),
                                                                  word_vectors_i[intuition_words[j],]),
                                                     term = power_words_dynamic[k],
                                                     term_threat = intuition_words[j]))
    }
  }
  
  df_weak_intuition <- data.frame()
  for(k in 1:length(weak_words_dynamic)){
    for(j in 1:length(intuition_words)){
      df_weak_intuition <- rbind(df_weak_intuition,
                                         data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                word_vectors_i[weak_words_dynamic[k],])),
                                                                 word_vectors_i[intuition_words[j],]),
                                                    term = weak_words_dynamic[k],
                                                    term_threat = intuition_words[j]))
    }
  }
  
  # --- power and deliberation
  df_power_deliberation <- data.frame()
  for(k in 1:length(power_words_dynamic)){
    for(j in 1:length(deliberation_words)){
      df_power_deliberation <- rbind(df_power_deliberation,
                                             data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                    word_vectors_i[power_words_dynamic[k],])),
                                                                     word_vectors_i[deliberation_words[j],]),
                                                        term = power_words_dynamic[k],
                                                        term_threat = deliberation_words[j]))
    }
  }
  
  df_weak_deliberation <- data.frame()
  for(k in 1:length(weak_words_dynamic)){
    for(j in 1:length(deliberation_words)){
      df_weak_deliberation <- rbind(df_weak_deliberation,
                                            data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                   word_vectors_i[weak_words_dynamic[k],])),
                                                                    word_vectors_i[deliberation_words[j],]),
                                                       term = weak_words_dynamic[k],
                                                       term_threat = deliberation_words[j]))
    }
  }
  
  results_frus <- rbind(results_frus,
                        data.frame(cos_est = c(mean(soviet_df_power$est - soviet_df_weak$est), 
                                               mean(china_df_power$est - china_df_weak$est),
                                               mean(vietcong_df_power$est - vietcong_df_weak$est),
                                               mean(df_power_intuition$est - df_weak_intuition$est), 
                                               mean(df_power_deliberation$est - df_weak_deliberation$est)),
                                   term = c("Soviet\nThreat", "China\nThreat", "Vietcong\nThreat", "Intuitive\nThinking", "Deliberative\nThinking"),
                                   resample_i = i))
  
}

# summarize results, taking second smallest and 19th largest estimates as CI endpoints
plot_df <- 
  results_frus %>% group_by(term) %>% 
  dplyr::summarise(mean_cos=mean(cos_est),
            ci_high = sort(cos_est)[19],
            ci_low = sort(cos_est)[2])
plot_df$model <- "main"
plot_df$term <- factor(plot_df$term, levels = c("Deliberative\nThinking", "Intuitive\nThinking",  "Vietcong\nThreat", "China\nThreat", "Soviet\nThreat"))

# --- mechanistic estimates (supplementary rise/decline analysis) --- #
# same ACDE estimates as above, but this time for dynamic power, as well as the deliberation terms in addition to intuition terms
threat_list <- list("Soviet\nThreat" = soviet_threat_words, 
                    "China\nThreat" = china_threat_words, 
                    "Vietcong\nThreat" = vietcong_threat_words)
results_frus <- data.frame()
for(p in 1:length(threat_list)){
  for(i in 1:length(files_list)){
    
    word_vectors_i <- readRDS(paste(file_direct,files_list[i], sep = ""))
    threat_words <- threat_list[[p]]

    # --- power and threat perception
    df_power <- data.frame()
    for(k in 1:length(power_words_dynamic)){
      for(j in 1:length(threat_words)){
        df_power <- rbind(df_power,
                                  data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                         word_vectors_i[power_words_dynamic[k],])),
                                                          word_vectors_i[threat_words[j],]),
                                             term = power_words_dynamic[k],
                                             term_threat = threat_words[j]))
      }
    }
    
    df_weak <- data.frame()
    for(k in 1:length(weak_words_dynamic)){
      for(j in 1:length(threat_words)){
        df_weak <- rbind(df_weak,
                                 data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                        word_vectors_i[weak_words_dynamic[k],])),
                                                         word_vectors_i[threat_words[j],]),
                                            term = weak_words_dynamic[k],
                                            term_threat = threat_words[j]))
      }
    }
    
    # --- power and threat perception, fixing intuition
    df_power_intuition <- data.frame()
    for(k in 1:length(power_words_dynamic)){
      for(j in 1:length(threat_words)){
        df_power_intuition <- rbind(df_power_intuition,
                                            data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                   colMeans(word_vectors_i[intuition_words,]),
                                                                                   word_vectors_i[power_words_dynamic[k],])),
                                                                    word_vectors_i[threat_words[j],]),
                                                       term = power_words_dynamic[k],
                                                       term_threat = threat_words[j]))
      }
    }
    
    df_weak_intuition <- data.frame()
    for(k in 1:length(weak_words_dynamic)){
      for(j in 1:length(threat_words)){
        df_weak_intuition <- rbind(df_weak_intuition,
                                           data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                  colMeans(word_vectors_i[intuition_words,]),
                                                                                  word_vectors_i[weak_words_dynamic[k],])),
                                                                   word_vectors_i[threat_words[j],]),
                                                      term = weak_words_dynamic[k],
                                                      term_threat = threat_words[j]))
      }
    }
    
    # --- power and threat perception, fixing deliberation
    df_power_deliberation <- data.frame()
    for(k in 1:length(power_words_dynamic)){
      for(j in 1:length(threat_words)){
        df_power_deliberation <- rbind(df_power_deliberation,
                                               data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                      colMeans(word_vectors_i[deliberation_words,]),
                                                                                      word_vectors_i[power_words_dynamic[k],])),
                                                                       word_vectors_i[threat_words[j],]),
                                                          term = power_words_dynamic[k],
                                                          term_threat = threat_words[j]))
      }
    }
    
    df_weak_deliberation <- data.frame()
    for(k in 1:length(weak_words_dynamic)){
      for(j in 1:length(threat_words)){
        df_weak_deliberation <- rbind(df_weak_deliberation,
                                              data.frame(est = cosine(colMeans(rbind(colMeans(word_vectors_i[our_words,]),
                                                                                     colMeans(word_vectors_i[deliberation_words,]),
                                                                                     word_vectors_i[weak_words_dynamic[k],])),
                                                                      word_vectors_i[threat_words[j],]),
                                                         term = weak_words_dynamic[k],
                                                         term_threat = threat_words[j]))
      }
    }
    
    results_frus <- rbind(results_frus,
                          data.frame(cos_est = c(mean(df_power$est - df_weak$est), 
                                                 mean(df_power_intuition$est - df_weak_intuition$est), 
                                                 mean(df_power_deliberation$est - df_weak_deliberation$est)),
                                     term = c("Threat Perception",  "Threat Perception +\nIntuitive Thinking", "Threat Perception +\nDeliberative Thinking"),
                                     threat_type = c(paste(names(threat_list)[p]), paste(names(threat_list)[p]), paste(names(threat_list)[p])),
                                     resample_i = i))
    
  }}

# summarize results, taking second smallest and 19th largest estimates as CI endpoints
plot_df2 <- 
  results_frus %>% group_by(term, threat_type) %>% 
  dplyr::summarise(mean_cos=mean(cos_est),
            ci_high = sort(cos_est)[19],
            ci_low = sort(cos_est)[2])
plot_df2$model <- "acde"
plot_df2$term <- factor(plot_df2$term, levels = c("Threat Perception +\nDeliberative Thinking", "Threat Perception +\nIntuitive Thinking", "Threat Perception"))

# --- format for plotting (figure A4A of the appendix)
plot_df$type <- c("Threat Perception", "Cognitive Style", "Cognitive Style", "Threat Perception", "Threat Perception")
plot_df$type <- factor(plot_df$type, levels = c("Threat Perception", "Cognitive Style"))
plot_df$major <- "Main Cosine\nEstimates"

plot_col <- ifelse(plot_df$type == "Threat Perception", "#54545d", "#99998e")
(p1 <- 
    ggplot(plot_df, aes(x = mean_cos, y = term, color = plot_col)) +
    geom_vline(xintercept = 0, linetype = "solid", color = "black", size = 1) +
    geom_linerange(aes(xmin = ci_low, xmax = ci_high), size = 3.2, color = plot_col) +
    geom_point(size = 7.5, color = "black") +
    geom_point(size = 6, color = plot_col) +
    facet_grid(type ~ major,scales = "free", space = "free_y") +
    theme_bw() +
    labs(x = "Sense of US Power", y=NULL) +
    theme(axis.title.x = element_text(size = 18, margin = margin(t=15)),
          axis.text.y = element_text(size = 18, color="black"),
          axis.text.x = element_text(size = 14),
          strip.text.x = element_text(size = 18, margin = margin(r=8,l=8,t=8,b=8)),
          strip.text.y = element_text(size = 17),
          legend.position = "none",
          panel.spacing = unit(1.2, "lines"),
          panel.background = element_rect(fill = "#f6f6f5"),
          strip.background = element_rect(fill = "gray90", color = "black", size = 1.5),
          plot.margin = margin(r = 8, l = 5, t = 5)))

# --- format for plotting (figure A4B of the appendix)
plot_df2$term <- factor(plot_df2$term, levels = rev(c("Threat Perception", "Threat Perception +\nIntuitive Thinking", "Threat Perception +\nDeliberative Thinking")))
plot_df2$threat_type <- factor(plot_df2$threat_type, levels = c("Vietcong\nThreat", "China\nThreat", "Soviet\nThreat"))
plot_df2$major <- "Estimates Fixing\nCognitive Style"
(p2 <- 
    ggplot(plot_df2, aes(x = mean_cos, y = threat_type, colour = term)) +
    geom_vline(xintercept = 0, linetype = "solid", color = "black", size = 1) +
    geom_linerange(aes(xmin = ci_low, xmax = ci_high), size = 3.2, position = position_dodge(width = .5)) +
    geom_point(size = 7.5, colour = "black", aes(group = term),  position = position_dodge(width = .5)) +
    geom_point(size = 6, position = position_dodge(width = .5)) +
    theme_bw() +
    scale_color_manual(values = c( "gray78", "#99998e", "#54545d")) +
    facet_wrap(major ~.) +
    labs(x = "Sense of US Power", y=NULL) +
    theme(axis.title.x = element_text(size = 18, margin = margin(t=15)),
          axis.text.y = element_text(size = 18, color="black"),
          axis.text.x = element_text(size = 14),
          strip.text.x = element_text(size = 18, margin = margin(r=8,l=8,t=8,b=8)),
          panel.spacing = unit(1.2, "lines"),
          legend.title = element_blank(),
          legend.text = element_text(size = 15),
          legend.spacing.y = unit(.5, "cm"),
          legend.justification = "top",
          panel.background = element_rect(fill = "#f6f6f5"),
          strip.background = element_rect(fill = "gray90", color = "black", size = 1.5),
          plot.margin = margin(l = 8, t = 5)) +
    guides(color = guide_legend(reverse = TRUE, byrow = TRUE)))

# --- appendix figure A4 
(final_p <- 
    ggpubr::ggarrange(p1, p2,
                      labels = c("A", "B"),
                      widths = c(.7,1),
                      font.label = list(face = "bold", size = 22),
                      hjust = -1,
                      ncol = 2, nrow = 1))

# --- appendix table A14
embedding_table_main_dynamic <- createTexreg(coef.names = c("Soviet Threat", "China Threat", "Vietcong Threat", "Intuitive Thinking", "Deliberative Thinking"),
                                            coef = c(subset(plot_df, term == "Soviet\nThreat")$mean_cos, subset(plot_df, term == "China\nThreat")$mean_cos, 
                                                     subset(plot_df, term == "Vietcong\nThreat")$mean_cos, subset(plot_df, term == "Intuitive\nThinking")$mean_cos, subset(plot_df, term == "Deliberative\nThinking")$mean_cos),
                                            ci.low = c(subset(plot_df, term == "Soviet\nThreat")$ci_low, subset(plot_df, term == "China\nThreat")$ci_low, 
                                                       subset(plot_df, term == "Vietcong\nThreat")$ci_low, subset(plot_df, term == "Intuitive\nThinking")$ci_low, subset(plot_df, term == "Deliberative\nThinking")$ci_low),
                                            ci.up = c(subset(plot_df, term == "Soviet\nThreat")$ci_high, subset(plot_df, term == "China\nThreat")$ci_high, 
                                                      subset(plot_df, term == "Vietcong\nThreat")$ci_high, subset(plot_df, term == "Intuitive\nThinking")$ci_high, subset(plot_df, term == "Deliberative\nThinking")$ci_high),
                                            model.name = "Main Cosine Estimates")
screenreg(embedding_table_main_dynamic,
          digits = 3)
# texreg(embedding_table_main_dynamic,
#        booktabs = T,
#        caption.above = T,
#        digits = 3,
#        caption = "Cosine Estimates: Relationship Between Dynamic Power Terms, Threat Perception, and Thinking Styles")

# --- appendix table A15
embedding_table_acde_dynamic_intuitive <- createTexreg(coef.names = c("Soviet Threat + Intuition", "China Threat + Intuition", "Vietcong Threat + Intuition"),
                                            coef = c(subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Soviet\nThreat")$mean_cos, 
                                                     subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "China\nThreat")$mean_cos, 
                                                     subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Vietcong\nThreat")$mean_cos),
                                            ci.low = c(subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Soviet\nThreat")$ci_low, 
                                                       subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "China\nThreat")$ci_low, 
                                                       subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Vietcong\nThreat")$ci_low),
                                            ci.up = c(subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Soviet\nThreat")$ci_high, 
                                                      subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "China\nThreat")$ci_high, 
                                                      subset(plot_df2, term == "Threat Perception +\nIntuitive Thinking" & threat_type == "Vietcong\nThreat")$ci_high),
                                            model.name = "Cosine Estimates Fixing Intuitive Thinking")
screenreg(embedding_table_acde_dynamic_intuitive,
          digits = 3)
# texreg(embedding_table_acde_dynamic_intuitive,
#        booktabs = T,
#        caption.above = T,
#        digits = 3,
#        caption = "Cosine Estimates: Relationship Between Dynamic Power Terms and Threat Perception, Fixing Intuitive Thinking Terms")

# --- appendix table A16
embedding_table_acde_dynamic_deliberative <- createTexreg(coef.names = c("Soviet Threat + Deliberation", "China Threat + Deliberation", "Vietcong Threat + Deliberation"),
                                                       coef = c(subset(plot_df2, term == "Threat Perception +\nDeliberative Thinking" & threat_type == "Soviet\nThreat")$mean_cos, 
                                                                subset(plot_df2, term == "Threat Perception +\nDeliberative Thinking" & threat_type == "China\nThreat")$mean_cos, 
                                                                subset(plot_df2, term == "Threat Perception +\nDeliberative Thinking" & threat_type == "Vietcong\nThreat")$mean_cos),
                                                       ci.low = c(subset(plot_df2, term == "Threat Perception +\nDeliberative Thinking" & threat_type == "Soviet\nThreat")$ci_low, 
                                                                  subset(plot_df2, term == "Threat Perception +\nDeliberative Thinking" & threat_type == "China\nThreat")$ci_low, 
                                                                  subset(plot_df2, term == "Threat Perception +\nDeliberative Thinking" & threat_type == "Vietcong\nThreat")$ci_low),
                                                       ci.up = c(subset(plot_df2, term == "Threat Perception +\nDeliberative Thinking" & threat_type == "Soviet\nThreat")$ci_high, 
                                                                 subset(plot_df2, term == "Threat Perception +\nDeliberative Thinking" & threat_type == "China\nThreat")$ci_high, 
                                                                 subset(plot_df2, term == "Threat Perception +\nDeliberative Thinking" & threat_type == "Vietcong\nThreat")$ci_high),
                                                       model.name = "Cosine Estimates Fixing Deliberative Thinking")
screenreg(embedding_table_acde_dynamic_deliberative,
          digits = 3)
# texreg(embedding_table_acde_dynamic_deliberative,
#        booktabs = T,
#        caption.above = T,
#        digits = 3,
#        caption = "Cosine Estimates: Relationship Between Dynamic Power Terms and Threat Perception, Fixing Deliberative Thinking Terms")

